var from_point;
var to_point;
var mapposition;
$(document).ready(function(){
	setmap();
	//点击保存按钮
	$("#savebutton a").click(function(){
		$.ajax({
			type: "POST",
			url: BASE_URL + "company/map_update",
			data: "mapposition=" + mapposition + "&_token=" + $("input[name=_token]").val() + "&id=" + $("input[name=id]").val(),
			dataType: "json",
			success: function(data){
				if(data.status == '200'){
					layer.msg("保存成功");
				}else{
					layer.msg("id未知错误");
				}
			},
			error: function(){
				layer.msg("网络原因错误");
			}
		});
	});
});

function setmap(){
	if($('#position').val() == '' || $('#position').val() == ' ' || $('#position').val() == 'null'){
		getMaps(longitude, latitude, address);
		$('#mright').html(address);
	}else{
		var areapos = $('#position').val().split(',');
		getMaps(areapos[0], areapos[1], areapos[2]);
		$('#mright').html(areapos[2]);
	}
}

//定位按钮
function searchMap(){
	var seaVal = ($('#seaVal').val() == '') ? address : $.trim($('#seaVal').val());
	$('#seaVal').val($.trim($('#seaVal').val())); //去除空格
	searchcon(seaVal);
	return false;
}

//搜索以后的值
function searchcon(seaVal){
	var map = new BMap.Map("mapshow");                     // 创建Map实例	
	map.enableScrollWheelZoom();					//鼠标大小缩放
	map.addControl(new BMap.NavigationControl());	//显示大小滚轮条
	var point = new BMap.Point(longitude, latitude);
	map.centerAndZoom(point, 15);					//以15的格式显示地图中心

	var local = new BMap.LocalSearch(map, {
		renderOptions: {map: map, panel: "mright"},
		pageCapacity: 10
	});
	local.search(seaVal);	//要搜索的值	

	//搜索以后获取列表内容的值
	local.setInfoHtmlSetCallback(function(poi){
		var marker = poi.marker;
		var pos = marker.getPosition();

		// 创建地理编码实例      
		var myGeo = new BMap.Geocoder();
		// 根据坐标得到地址描述    
		myGeo.getLocation(new BMap.Point(pos.lng, pos.lat), function(rs){
			var addComplete = rs.addressComponents;
			var newAdress = addComplete.district + addComplete.street + addComplete.streetNumber;	//获取地址
			getMaps(pos.lng, pos.lat, newAdress);
		});
	});
}


//绘制地图定位函数
function getMaps(lng, lat, position){
	var map = new BMap.Map("mapshow");					//显示位置
	map.enableScrollWheelZoom();					//鼠标大小缩放
	map.addControl(new BMap.NavigationControl());	//显示大小滚轮条
	var point = new BMap.Point(lng, lat);
	map.centerAndZoom(point, 15);					//以15的格式显示地图中心店


	var markerls = new BMap.Marker(point);				//新建标注
	var infoWindow = new BMap.InfoWindow("<span style=\"font-size:14px;\">当前位置:" + position + "<br><span style=\"font-size:12px; line-height:24px;\">(提示:任意点击地图,选择您的位置)</span></span>");
	map.openInfoWindow(infoWindow, point);			//默认时，显示窗口信息
	mapposition = lng + ',' + lat + ',' + position;
	$('#maplng').html(lng);
	$('#maplat').html(lat);
	//$('#position').val(lng+','+lat+','+position);
	markerls.addEventListener("click", function(){
		map.openInfoWindow(infoWindow, point);
	});												//点击标注点时显示窗口信息

	markerls.enableDragging(true); 					//启用地图鼠标拖拽
	map.addOverlay(markerls);						//添加标注点在地图上

	//创建地理编码
	var geoc = new BMap.Geocoder();
	map.addEventListener('click', function(e){		//点击地图任意位置事件触发
		map.removeOverlay(markerls);				//点击地图任意点时，删除对象地图上的markerls标注点
		markerls = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));  //新建地图markerls
		map.addOverlay(markerls);					//添加新的标注点markerls		

		var pt = e.point;							//获取新的经纬度
		geoc.getLocation(pt, function(rs){			//根据坐标得到地址描述			
			var addComp = rs.addressComponents;
			var address = addComp.district + addComp.street + addComp.streetNumber;	//获取地址
			//var address=rs.address;					//获取地址
			mapposition = pt.lng + ',' + pt.lat + ',' + address;
			$('#maplng').html(pt.lng);
			$('#maplat').html(pt.lat);
			//$('#position').val(pt.lng+','+pt.lat+','+address);
			var infoWindow_new = new BMap.InfoWindow("<span style=\"font-size:14px;\">当前位置:" + address + "<br><span style=\"font-size:12px; line-height:24px;\">(提示:任意点击地图,选择您的位置)</span></span>");
			map.openInfoWindow(infoWindow_new, new BMap.Point(pt.lng, pt.lat));
		});
	});
}



